DAY37:Find the odd int


Posted by birdbirdmurmur on 2023-08-19

題目連結

https://www.codewars.com/kata/54da5a58ea159efa38000836

解法

function findOdd(A) {
    let count = {}
    let min = Infinity

    for (const num of A) {
        count[num] ? count[num]++ : count[num] = 1
    }

    for (const num in count) {
        if (count[num] % 2 !== 0 && min > parseInt(num)) {
            min = parseInt(num)
        }
    }

    return min === Infinity ? 0 : min
}

筆記

同時練習for...offor...in兩種用法

  1. for...of用於迭代陣列等可迭代物件的
  2. for...in用於迭代物件的屬性,包括可數的屬性。

#javascript #Codewars #for...in #for...of #object







Related Posts

AppWorks School Batch #16 Front-End Class 學習筆記&心得(駐點階段四:個人專案~重構)

AppWorks School Batch #16 Front-End Class 學習筆記&心得(駐點階段四:個人專案~重構)

[MSSQL] 常用預設資料庫('.', LocalDB...)

[MSSQL] 常用預設資料庫('.', LocalDB...)

[http] 不只是簡單的跨域請求 Preflight Request

[http] 不只是簡單的跨域請求 Preflight Request


Comments